package aooO.crm.Controller;

import aooO.crm.Service.*;
import aooO.crm.common.Utils.DateUtils;
import aooO.crm.common.Utils.UUIDUtils;

import aooO.crm.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("workbench/clue")
public class clueRemarkController {

    @Autowired
    ClueRemarkService clueRemarkService;

    @Autowired
    ActivityService activityService;

    @Autowired
    ClueActivityRelationService clueActivityRelationService;

    @Autowired
    ClueService clueService;

    @Autowired
    DicValueService dicValueService;

//    url:'workbench/clue/saveCreateClueRemark.do'
    @RequestMapping("/saveCreateClueRemark.do")
    @ResponseBody
    public Object saveCreateClueRemark(ClueRemark clueRemark, HttpSession session){
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        try {
            //进行保存操作
//            data:{
//            noteContent:noteContent,
//            clueId:clueId
//            },
            clueRemark.setId(UUIDUtils.uuid());
            User user=(User) session.getAttribute(stateCode.SESSION_USER);
            clueRemark.setCreateBy(user.getId());
            clueRemark.setCreateTime(DateUtils.FormatDateTime(new Date()));
            clueRemark.setEditFlag("0");

            int i = clueRemarkService.insertClueRemark(clueRemark);
            if (i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
                msg.setReturnData(clueRemark);
            }else {
                msg.setMessage("保存失败！请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("保存失败！请重试！");
        }
        return msg;
    }

//    'workbench/clue/saveEditClueRemark.do'
    @RequestMapping("/saveEditClueRemark.do")
    @ResponseBody
    public Object saveEditClueRemark(ClueRemark clueRemark,HttpSession session){
//        id:id,  noteContent:noteContent
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        //update
        try {
            User user=(User) session.getAttribute(stateCode.SESSION_USER);
            clueRemark.setEditBy(user.getId());
            clueRemark.setEditTime(DateUtils.FormatDateTime(new Date()));
            clueRemark.setEditFlag("1");

            int i = clueRemarkService.updateClueRemark(clueRemark);
            if (i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
                msg.setReturnData(clueRemark);
            }else {
                msg.setMessage("修改失败！请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("修改失败！请重试！");
        }
        return msg;
    }

//    workbench/clue/deleteClueRemarkById.do
    @RequestMapping("/deleteClueRemarkById.do")
    @ResponseBody
    public Object deleteClueRemarkById(String id){
//        id:id
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        try {
            int i = clueRemarkService.deleteClueRemarkById(id);
            if (i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
            }else {
                msg.setMessage("删除失败！请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("删除失败！请重试！");
        }
        return msg;
    }

    //    workbench/clue/saveBound.do
    @RequestMapping("/saveBound.do")
    @ResponseBody
    public Object saveBound(String[] activityId,String clueId){
//        data:ids
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        //处理添加为list
        //添加选择关联的市场活动
        List<ClueActivityRelation> list=new ArrayList<>();
        ClueActivityRelation relation;
        for (int i=0;i<activityId.length;i++) {
            //遍历数组，整合到集合中
            relation=new ClueActivityRelation();
            relation.setId(UUIDUtils.uuid());
            relation.setActivityId(activityId[i]);
            relation.setClueId(clueId);
            list.add(relation);
        }
        try{
            int i = clueActivityRelationService.insertBound(list);
            if(i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
                List<Activity> activityList = activityService.selectActivitiesByIds(activityId);
                msg.setReturnData(activityList);
            }else {
                msg.setMessage("关联失败，请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("关联失败，请重试！");
        }
        return msg;
    }

//    'workbench/clue/queryActivityForDetailByNameAndClueId.do'
//    activityName:activityName,
//    clueId:clueId
    //搜索市场活动
    @RequestMapping("/queryActivityForDetailByNameAndClueId.do")
    @ResponseBody
    public Object queryActivityForDetailByNameAndClueId(String activityName,String clueId){
        //根据活动名称模糊查询，关联线索的活动
        Map<String,Object> map=new HashMap<>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        List<Activity> activityList = activityService.queryActivityForDetailByNameAndClueId(map);
        return activityList;
    }

//    'workbench/clue/saveUnbound.do',
    @RequestMapping("/saveUnbound.do")
    @ResponseBody
    public Object saveUnbound(ClueActivityRelation car){
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        String activityId = car.getActivityId();
        String clueId = car.getClueId();
        try{
            int i = clueActivityRelationService.deleteBound(car);
            if (i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
            }else {
                msg.setMessage("解除失败！请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("接触失败！请重试！");
        }

        return msg;
    }

//'workbench/clue/toConvert.do?id=
    @RequestMapping("/toConvert.do")
    public String toConvert(String id, HttpServletRequest request){
        //跳转到转换页
        Clue clue = clueService.selectDetailClueById(id);
//        stageList
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");
        request.setAttribute("clue",clue);
        request.setAttribute("stageList",stageList);
        return "/workbench/clue/convert";
    }

//    'workbench/clue/queryActivityForConvertByNameAndClueId.do'
    //activityName:activityName,
    //					clueId:clueId

    @RequestMapping("/queryActivityForConvertByNameAndClueId.do")
    @ResponseBody
    public Object queryActivityForConvertByNameAndClueId(String activityName,String clueId){
        //根据活动名称模糊查询，关联线索的活动
        //找的是与线索关联过的市场活动
        Map<String,Object> map=new HashMap<>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        List<Activity> activityList = activityService.queryActivityForConvertByNameAndClueId(map);
        return activityList;
    }

    //    'workbench/clue/convertClue.do',   线索转换
    @RequestMapping("/convertClue.do")
    @ResponseBody
    public Object convertClue(String clueId,String money,String name,String expectedDate,String stage,String activityId,String isCreateTran,HttpSession session){

        Map<String,Object> map=new HashMap<>();
        map.put("clueId",clueId);
        map.put("money",money);
        map.put("name",name);
        map.put("expectedDate",expectedDate);
        map.put("stage",stage);
        map.put("activityId",activityId);
        map.put("isCreateTran",isCreateTran);

        User user= (User)session.getAttribute(stateCode.SESSION_USER);
        map.put("user",user);
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");

        //开始事务 放到一个service中
        try{
            clueService.saveConvert(map);
            msg.setCode(stateCode.STATE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("操作失败，请重试！");
        }
        return msg;
    }
}
